<?php

class Paging {
    #create page

    function createPage($jumlah, $offset, $limit, $nextPage, $pagePerCols, $colPage) {
        $csd = $_GET["csd"];
        $csf = $_GET["csf"];
        $var = $_GET["v"];

        $addLink = "";
        /*
          if($csd!="" && $csf!="")
          $addLink = "&csf=". $csf ."&csd=". $csd;
         */

        $arr_pg = array("os", "np", "cp");
        $temp_g = $_GET;
        if (is_array($arr_pg)) {
            foreach ($arr_pg as $key) {
                if (isset($temp_g[$key]))
                    unset($temp_g[$key]);
            }
        }

        foreach ($temp_g as $key2 => $element) {
            if (is_array($element)) {
                foreach ($element as $key3 => $element3) {
                    $arr[] = $key2 . '[' . $key3 . ']=' . $element3;
                }
            } else {
                $arr[] = $key2 . '=' . $element;
            }
        }
        if (is_array($arr))
            $addLink = "&" . implode('&', $arr);
        else
            $addLink = "";

        $hasil = "";

        if ($jumlah > 0)
            $jumPage = ceil(($jumlah / $limit));
        else
            $jumPage = 1;

        $nPage = $nextPage;
        $jumRec = $limit * $pagePerCols;
        if ($offset != 0)
            $hasil .= "<li class=\"previous\"><a class=\"paging\" " . "href=\"?os=0&np=1&cp=1" . $addLink . "&v=" . $var . " \">&laquo; First</a><li>";
        else
            $hasil .= "<li class=\"previous-off\">&laquo; First</li>";
        if ($nPage >= $pagePerCols && $nPage > 1)
            $hasil .= "<li class=\"previous\"><a class=\"paging\" " . "href=\"?os=" . (($pagePerCols * $limit * ($colPage - 1)) - $limit) . "&np=" . ($nPage - $pagePerCols) . "&cp=" . ($colPage - 1) . $addLink . " &v=" . $var . "\">&laquo; Previous</a><li>";


        if ($jumlah > $limit) {
            for ($i = 0; $i < $pagePerCols; $i++) {
                if ($jumlah > (($i + ($nPage - 1)) * $limit))
                    $hasil .= "" . ($offset != ($i + ($nPage - 1)) * $limit ? "<li><a href=\"?os=" . (($i + ($nPage - 1)) * $limit) . "&np=" .
                                    $nPage . "&cp=" . $colPage . $addLink . " \">" . ($i + $nPage) . "</a></li>" : "<li class=\"active\">" . ($i + $nPage) . "</li>");
            }
        }
        else {
            $hasil .= "<li class=\"active\">1</li>";
        }

        if ($pagePerCols * $colPage < $jumPage && $jumlah > $limit * $pagePerCols)
            $hasil .= "<li class=\"next\"><a class=\"paging\" href=\"?" . $addLink . "&os=" . ($pagePerCols * $limit * ($colPage)) . "&np=" . ($nPage + $pagePerCols) . "&cp=" . ($colPage + 1) . "&v=" . $var . "\">Next &raquo;</a></li>";

        if ($jumlah > $offset + $limit) {
            $hasil .= "<li class=\"next\"> ";
            $os = (($jumlah / $limit) > $jumPage ? ($jumPage * $limit) : ($jumPage - 1) * $limit);
            $hasil .= "<a class=\"paging\" href=\"?os= " . (($jumlah / $limit) > $jumPage ? ($jumPage * $limit) : ($jumPage - 1) * $limit) . "&np=" . $nPage . "&cp=" . $colPage . $addLink . "&v=" . $var . "\">Last &raquo;</a><li>";
        }else
            $hasil .= "<li class=\"next-off\">Last &raquo;</li>";

        if ($hasil != "") {
            $hasil = "<ul id=\"pagination-clean\">" . $hasil . "</ul>";
        }

        return $hasil;
    }

}

?>
